let Father = require('./Father');
let model = require('../../model/base')
let mysql = require('mysql')

class Son extends Father {
    async index() {
        let sql = 'select * from china';
        let model1 = new model();
        let result = await model1.select(sql, '');
        console.log(result);
        return this.nunjucks.render('demo.html', { result });
    }
    async china() {
        let request = require("request");
        let yiqingapi = "https://interface.sina.cn/news/wap/fymap2020_data.d.json";
        let fs = require('fs');
        request.get(yiqingapi, {}, function (err, result) {
            if (err) {
                console.log(err);
            } else {
                let obj = JSON.parse(result.body);
                obj.data.list.forEach(element => {
                    let name = element.name;
                    let susNum = element.susNum;
                    let econNum = element.econNum;
                    let cureNum = element.cureNum;
                    let deathNum = element.deathNum;

                    let db2 = mysql.createConnection({
                        host: 'localhost',
                        user: 'root',
                        password: '123456',
                        database: 'table',
                        port: '3306'
                    })
                    db2.connect();
                    let selectSql = `select * from china where city = '${name}'`;
                    db2.query(selectSql, function (err, result) {
                        if (err) {
                            console.log(err.message);
                        } else {
                            if (result.length == 0) {
                                let addSql = `INSERT INTO china (city, addnum, nums, cure,dead) VALUES ('${name}', '${susNum}', '${econNum}', '${cureNum}', '${deathNum}')`
                                db2.query(addSql, function (err, result) {
                                    if (err) {
                                        console.log('插入失败');
                                        console.log(err.message);
                                    } else {
                                        console.log('插入成功');
                                    }
                                })
                            } else {
                                let updateSql = `update china set city = '${name}',addnum='${susNum}',nums='${econNum}',cure='${cureNum}',dead='${deathNum}'`;
                                db2.query(updateSql, function (err, result) {
                                    if (err) {
                                        console.log('更新失败');
                                    } else {
                                        console.log('更新成功');
                                    }
                                })
                            }
                        }
                    });
                });
            }

        });
    }
}
module.exports = Son;